/**
 * auth: zhukaijun
 * date: 2022-3-11
 * des: deep clone
 */
export function cloneLoop(x) {
    const root = {}, loopList = [
        {
            parent: root,
            key: undefined,
            data: x,
        }
    ];
    while (loopList.length) {
        const node = loopList.pop(), parent = node.parent, key = node.key, data = node.data;
        let res = parent;
        if (typeof key !== 'undefined') {
            res = parent[key] = {};
        }

        for (let k in data) {
            if (data.hasOwnProperty(k)) {
                if (typeof data[k] === 'object') {
                    // 下一次循环
                    loopList.push({
                        parent: res,
                        key: k,
                        data: data[k],
                    });
                } else {
                    res[k] = data[k];
                }
            }
        }
    }
    return root;
}

export function dataEncode(data) {
    if (typeof data === 'object') {
        return encodeURIComponent(JSON.stringify(data));
    }
    return data;
}

export function dataDecode(data) {
    return decodeURIComponent(data);
}